﻿//
// *************************************************************************************
// WP-Framework
//
// Developed by 
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2012 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//
			

using System;
using System.Linq.Expressions;
using Expression = System.Linq.Expressions.Expression;
using KretschIT.WP_Fx.Core.Validation;
using KretschIT.WP_Fx.Core.Extensions;
using KretschIT.WP_Fx.EasyMoq.I18n;

namespace KretschIT.WP_Fx.EasyMoq.Calls.Arguments
{
    public class Argument : IArgument
    {
        private Expression expression;

        public Argument(Expression expression)
        {
            It.IsNotNull(() => expression);
            It.IsValid(() => expression, e => e is ConstantExpression || e is MemberExpression);
            
            this.expression = expression;
            this.GetValueOf(expression);           
        }

        private void GetValueOf(Expression expression)
        {
            if (expression is ConstantExpression)
            {
                this.Value = ((ConstantExpression)expression).Value;
            }
            else if (expression is MemberExpression)
            {
                this.Value = ((MemberExpression)expression).GetValue();
            }
            else
            {
                throw new NotSupportedException(string.Format(Messages.Exception_NotSupportedExpressionFormat, expression.GetType().FullName));
            }
        }

        public Type Type
        {
            get { return this.expression.Type; }
        }

        public object Value { get; private set; }

        public bool IsDefaultValue
        {
            get 
            {
                var defaultValue = this.Type.GetDefaultValue();
                return defaultValue != null ? defaultValue.Equals(this.Value) : this.Value == null; 
            }
        }
    }
}
